package cfca.paperless.client.servlet.customer;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.StringUtil;
/**
 * 佰仟金融无纸化（挑战码）性能测试
 * 一码一签、一码多签、多码多签
 * @author clsu
 *
 */
public class testCompoundSealAutoPdfList_billionsfinance2 {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
            
            String userAuthInfo = createUserAuthInfoXml("张三", "Z", "ABC1DDD111", "602d5275");
            String userAuthInfo2 = createUserAuthInfoXml("李四", "Z", "ABC1DDD222", "6eba0851");
            String compoundSealStrategyXml = "<List>" + userAuthInfo + "" + "</List>";
          
            String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();
            String sealedPdf = clientBean.compoundSealAutoPdfList("", "", compoundSealStrategyXml, PaperlessConfig.operatorCode, channelBeanXml);
            String code = StringUtil.getNodeText(sealedPdf, "Code");
            //返回值Code等于200代表后台处理成功
            if (code.equals("200")) {
                List<String[]> list = getPdfBeanArr(sealedPdf);
                for (int i = 0; i < list.size(); i++) {
                    String[] strArr = list.get(i);
                        IoUtil.write("./TestData/output/compoundSealAutoPdfList-" + i + ".pdf", Base64.decode(strArr[0].getBytes()));
                        System.out.println("bizSerialNo=" + strArr[1]);
                        System.out.println("proofPdfId=" + strArr[2]);

                }

            } else {
                System.out.println(sealedPdf);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("time:" + (System.currentTimeMillis() - start));
    }

    private static List<String[]> getPdfBeanArr(String pdfs) {
        List<String[]> list = new ArrayList<String[]>();
        Object pdfArr[] = StringUtil.getXmlFieldArr(pdfs, "PdfBean");
        String authCode = StringUtil.getXmlField(pdfs, "AuthCode");
        System.out.println(authCode);
        if (pdfArr != null && pdfArr.length > 0) {
            for (int i = 0; i < pdfArr.length; i++) {
                String pdfBeanString = "<PdfBean>" + pdfArr[i].toString() + "</PdfBean>";
                String pdf = StringUtil.getXmlField(pdfBeanString, "Pdf");
                String bizSerialNo = StringUtil.getXmlField(pdfBeanString, "BizSerialNo");
                if (StringUtil.isEmpty(bizSerialNo)) {
                    bizSerialNo = "";
                }
                String proofPdfId = StringUtil.getXmlField(pdfBeanString, "ProofPdfId");
                if (StringUtil.isEmpty(proofPdfId)) {
                    proofPdfId = "";
                }

                list.add(new String[] { pdf, bizSerialNo, proofPdfId, authCode});
            }
        }
        return list;
    }
    
    public static String createUserAuthInfoXml(String userName, String identificationType, String identificationNo, String authCodeValue) throws Exception {
        
        StringBuilder xml = new StringBuilder("<UserAuthInfo>");
        xml.append("<UserName>" + userName + "</UserName>");
        xml.append("<IdentificationType>" + identificationType + "</IdentificationType>");
        xml.append("<IdentificationNo>" + identificationNo + "</IdentificationNo>");
        xml.append("<AuthCodeValue>" + authCodeValue + "</AuthCodeValue>");
        xml.append("</UserAuthInfo>");

        return xml.toString();
    }
}
